#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int n,a[2005],dp[2005][2005];
signed main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int T;
    cin>>T;
    while(T--)
    {
        memset(a,0,sizeof(a));
        memset(dp,0,sizeof(dp));
    cin>>n;
    if (n>2000)return 0;
    for (int i=1;i<=n;i++)cin>>a[i];
    for (int i=1;i<=n;i++)
    {
        for (int j=0;j<=i-1;j++)
        {
            dp[i][j]=max(dp[i-1][j],dp[i][j]);
            dp[i][i-1]=max(dp[i][i-1],dp[i-1][j]);
            if (a[i]==a[i-1])dp[i][j]=max(dp[i][j],dp[i-1][j]+a[i]);
            if (a[i]==a[j])
            {
                dp[i][i-1]=max(dp[i][i-1],dp[i-1][j]+a[i]);
            }
        }
    }
    int ans=0;
    for (int i=0;i<=n-1;i++)ans=max(ans,dp[n][i]);
    cout<<ans<<endl;
    }
    return 0;
}
